# 使用Ubuntu 22.04作为基础镜像
FROM ccr.ccs.tencentyun.com/library/ubuntu:22.04

# 设置环境变量
ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PATH="/opt/miniconda3/bin:$PATH" \
    CONDA_DEFAULT_ENV=deploy

# 设置工作目录
WORKDIR /app

# 根据架构配置不同的apt源和安装基础软件包
RUN ARCH=$(uname -m) && \
    if [ "$ARCH" = "x86_64" ]; then \
        echo "配置x86_64架构 - 使用清华镜像源加速" && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list; \
    elif [ "$ARCH" = "aarch64" ]; then \
        echo "配置ARM64架构 - 使用官方源确保稳定性"; \
    else \
        echo "未知架构: $ARCH - 使用默认源"; \
    fi && \
    apt-get update && apt-get install -y \
    ca-certificates \
    curl \
    wget \
    openssh-server \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# 下载并安装Miniconda（自动检测架构）
RUN ARCH=$(uname -m) && \
    if [ "$ARCH" = "x86_64" ]; then \
        wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh; \
    elif [ "$ARCH" = "aarch64" ]; then \
        wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh -O /tmp/miniconda.sh; \
    else \
        echo "Unsupported architecture: $ARCH" && exit 1; \
    fi && \
    bash /tmp/miniconda.sh -b -p /opt/miniconda3 && \
    rm /tmp/miniconda.sh

# 初始化conda
RUN /opt/miniconda3/bin/conda init bash

# 创建deploy虚拟环境
RUN /opt/miniconda3/bin/conda create --name deploy python=3.12 -c conda-forge --override-channels -y

# 激活deploy环境并配置pip使用清华镜像源
RUN /opt/miniconda3/bin/conda run -n deploy pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 复制项目文件
COPY . .

# 在deploy环境中安装Python依赖
RUN /opt/miniconda3/bin/conda run -n deploy pip install --no-cache-dir --root-user-action=ignore -r requirements.txt

# 配置SSH服务
RUN mkdir /var/run/sshd && \
    echo 'root:123' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
    sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config

# # 创建非root用户
# RUN useradd --create-home --shell /bin/bash app \
#     && chown -R app:app /app

# # 切换到非root用户
# USER app




# 暴露端口
EXPOSE ${APP_PORT:-5090}

# 创建启动脚本
RUN echo '#!/bin/bash\n\
# 启动SSH服务\n\
service ssh start\n\
\n\
# 启动应用服务\n\
/opt/miniconda3/bin/conda run -n deploy gunicorn --bind 0.0.0.0:${APP_PORT:-5090} --workers ${WORKERS:-4} --timeout ${TIMEOUT:-120} app:app\n\
' > /start.sh && chmod +x /start.sh

# 启动命令
CMD ["/start.sh"]
